﻿#region Vương văn hùng
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;

namespace BusinessLogic
{
    public static class ChiTietHoaDonNhap
    {
        public static Entities.ChiTietHoaDonNhap[] SelectChiTietHoaDonNhap(Entities.ChiTietHoaDonNhap hoadon)
        {
            Entities.ChiTietHoaDonNhap[] list = null;
            try
            {
                //lay mo ket noi voi' chuoi~ ket noi lay' tu` file app.confg trong GUI
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.ChiTietHoaDonNhap.sp_Select_ChiTietHoaDonNhap, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.HanhDong, SqlDbType.NVarChar).Value = hoadon.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.MaHoaDonNhap, SqlDbType.VarChar).Value = hoadon.MaHoaDonNhap;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.MaHangHoa, SqlDbType.VarChar).Value = hoadon.MaHangHoa;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.ChiTietHoaDonNhap row = new Entities.ChiTietHoaDonNhap();
                    row.MaHoaDonNhap = dr[Common.Constants.ChiTietHoaDonNhap.MaHoaDonNhap].ToString();
                    row.MaHangHoa = dr[Common.Constants.ChiTietHoaDonNhap.MaHangHoa].ToString();
                    row.TenHangHoa = dr[Common.Constants.ChiTietHoaDonNhap.TenHangHoa].ToString();
                    row.SoLuong =dr[Common.Constants.ChiTietHoaDonNhap.SoLuong].ToString();
                    row.PhanTramChietKhau = dr[Common.Constants.ChiTietHoaDonNhap.PhanTramChietKhau].ToString();
                    row.DonGia = dr[Common.Constants.ChiTietHoaDonNhap.DonGia].ToString();
                    row.ThanhTien = dr[Common.Constants.ChiTietHoaDonNhap.ThanhTien].ToString();
                    row.Thue = dr[Common.Constants.ChiTietHoaDonNhap.Thue].ToString();
                    row.SoLuong = dr[Common.Constants.ChiTietHoaDonNhap.SoLuong].ToString();
                    row.HanSuDung = DateTime.Parse(dr[Common.Constants.ChiTietHoaDonNhap.HanSuDung].ToString());
                    row.GhiChu = dr[Common.Constants.ChiTietHoaDonNhap.GhiChu].ToString();
                    row.SoLuongConLai = int.Parse(dr[Common.Constants.ChiTietHoaDonNhap.SoLuongConLai].ToString());
                    row.Deleted = Boolean.Parse(dr[Common.Constants.ChiTietHoaDonNhap.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.ChiTietHoaDonNhap[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.ChiTietHoaDonNhap)arr[i];
                    }
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                list = null;
            }
            return list;
        }

        public static int InsertUpdateListChiTietHoaDonNhap(Entities.ChiTietHoaDonNhap[] chitiet)
        {
            int rt = 0;
            try
            {
                for (int i = 0; i < chitiet.Length; i++)
                {
                    rt = InsertUpdateChiTietHoaDonNhap(chitiet[i]);
                    if (rt == 0)
                    {
                        break;
                    }
                    else
                    { continue; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                rt = 0;
            }
            return rt;
        }

        public static int InsertUpdateChiTietHoaDonNhap(Entities.ChiTietHoaDonNhap chitiet)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.ChiTietHoaDonNhap.sp_InsertUpdate_ChiTietHoaDonNhap, cn);
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.HanhDong, SqlDbType.NVarChar).Value = chitiet.HanhDong;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.MaHoaDonNhap, SqlDbType.VarChar).Value = chitiet.MaHoaDonNhap;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.MaHangHoa, SqlDbType.VarChar).Value = chitiet.MaHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.TenHangHoa, SqlDbType.NVarChar).Value = chitiet.TenHangHoa;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.SoLuong, SqlDbType.Float).Value = chitiet.SoLuong;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.PhanTramChietKhau, SqlDbType.Float).Value = chitiet.PhanTramChietKhau;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.DonGia, SqlDbType.Float).Value = chitiet.DonGia;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.ThanhTien, SqlDbType.Float).Value = chitiet.ThanhTien;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.Thue, SqlDbType.Float).Value = chitiet.Thue;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.SoLuongConLai, SqlDbType.Float).Value = chitiet.SoLuongConLai;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.HanSuDung, SqlDbType.DateTime).Value = chitiet.HanSuDung;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.MaDonViTinh, SqlDbType.VarChar).Value = chitiet.MaDonViTinh;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.TenDonViTinh, SqlDbType.NVarChar).Value = chitiet.TenDonViTinh;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.GhiChu, SqlDbType.NVarChar).Value = chitiet.GhiChu;
                cmd.Parameters.Add(Common.Constants.ChiTietHoaDonNhap.Deleted, SqlDbType.Bit).Value = chitiet.Deleted;
                rt = cmd.ExecuteNonQuery();
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                rt = 0;
            }
            return rt;
        }
    }
}
#endregion
